package com.foxit.setting;

import java.io.*;
import java.net.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.text.SimpleDateFormat;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class VerifySetting extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       request.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       out.println("<HTML><head><style>table,th,td { border:1px solid black;border-collapse:collapse; } </style></head><body>");
       String date = request.getParameter("verify");
       if(date==null || date.length()==0) {
          out.println("verify date is empty</body></HTML>");
          return;
       }
       out.println("<table border='1' style='width:300px'><tr><th colspan=3>FOXIT Setting Tool(驗證設定)</th></tr>");
       out.println("<tr><th>table</th><th>item</th><th>message</th></tr>");
       try {
          //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
          //sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
          //Date date = sdf.parse(d);
          MongoClient mongo = new MongoClient("192.201.108.97", 27017);
          DB db = mongo.getDB("settingdb");
          DBCollection t_adid = db.getCollection("adid");
          DBCollection t_adrule = db.getCollection("adrule");
          DBCollection t_top = db.getCollection("top");
          DBCollection t_action = db.getCollection("action");
          DBCollection t_adana = db.getCollection("adana");
          DBCollection t_adanarule = db.getCollection("adanarule");
          DBCollection t_default = db.getCollection("default");
          DBCollection t_groupad = db.getCollection("groupad");
          DBCollection t_grouptype = db.getCollection("grouptype");
          DBCollection t_impmsgad = db.getCollection("impmsgad");
          DBCollection t_policy = db.getCollection("policy");
          DBCollection t_round = db.getCollection("round");
          DBCollection t_member = db.getCollection("member");
          DBCollection t_server = db.getCollection("server");
          BasicDBObject query = new BasicDBObject("start",date);
          int i=0, j=0;
// AD
          DBCursor cursor = t_adid.find(query);
          while (cursor.hasNext()) {
             Adid s = (Adid) JSON.parseObject(cursor.next().toString(),Adid.class);
             DBObject myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",s.getAdrule()));
             if(myDoc == null) {
                out.println("<tr><td>adid</td><td>adid="+s.getAdid()+"</td><td>adrule="+s.getAdrule()+" not found</td></tr>");
             }
          }
          cursor.close();
// ADREULE
          cursor = t_adrule.find(query);
          while (cursor.hasNext()) {
             Adrule s = (Adrule) JSON.parseObject(cursor.next().toString(),Adrule.class);
             DBObject myDoc= t_adid.findOne(new BasicDBObject("start",date).append("adrule",s.getAdrule()));
             if(myDoc == null) {
                out.println("<tr><td>adrule</td><td>adrule="+s.getAdrule()+"</td><td>adid not found</td></tr>");
             }
          }
          cursor.close();
// MEMBER
          cursor = t_member.find(query);
          while (cursor.hasNext()) {
            Member s = (Member) JSON.parseObject(cursor.next().toString(),Member.class);
            DBObject p = t_policy.findOne(new BasicDBObject("start",date).append("type",s.getType()));
            if(p == null) {
               out.println("<tr><td>member</td><td>member="+s.getMember()+"</td><td>type="+s.getType()+" not found</td></tr>");
            }
          } 
          cursor.close();
// SERVER
          List list=(List) t_server.distinct("server");
          for(i =0;i < list.size();i++) {
            BasicDBObject query1 = new BasicDBObject("start",date).append("server",list.get(i));
            cursor = t_server.find(query1);
            while (cursor.hasNext()) {
               Server s = (Server) JSON.parseObject(cursor.next().toString(),Server.class);
               for(j=0;j < s.getGroupno();j++) {
                  DBObject p = t_grouptype.findOne(new BasicDBObject("start",date).append("server",s.getServer()).append("show_mode",s.getShow_mode()).append("groupno",j+1));
                  if(p==null) {
                     out.println("<tr><td>server="+s.getServer()+"</td><td>show_mode="+s.getShow_mode()+"</td><td>groupno="+j+" not found in policy</td></tr>");
                  } else {
                     Grouptype r = (Grouptype) JSON.parseObject(p.toString(),Grouptype.class);
                     if(r.getType()==2) {
                        String[] adl=r.getAdrule().split(",");
                        for(String ad:adl) {
                           DBObject myDoc= t_action.findOne(new BasicDBObject("start",date).append("name",ad));
                           if(myDoc == null) {
                              out.println("<tr><td>server="+s.getServer()+"</td><td>show_mode="+s.getShow_mode()+"</td><td>action="+ad+" not found</td></tr>");
                           }
                        } 
                     } else {
                        DBObject myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",r.getAdrule()));
                        if(myDoc == null) {
                           myDoc= t_round.findOne(new BasicDBObject("start",date).append("name",r.getAdrule()));
                           if(myDoc == null) {
                              out.println("<tr><td>server="+s.getServer()+"</td><td>show_mode="+s.getShow_mode()+"</td><td>adrule or round="+r.getAdrule()+" not found</td></tr>");
                           }
                        }
                     }
                  }
               }
            }
          }
          cursor.close();
// DEFAULTAD
          cursor = t_default.find(query);
          while (cursor.hasNext()) {
             Default s = (Default) JSON.parseObject(cursor.next().toString(),Default.class);
             String[] adlist=s.getAdlist().split(",");
             for(String ad:adlist) {
                 DBObject myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",ad)); 
                 if(myDoc == null) {
                    out.write("<tr><td>defaultad</td><td>defname="+s.getName()+"</td><td>adrule="+ad+"</td></tr>");
                 }
             }
          }
          cursor.close();
// IMPMSGAD
          cursor = t_impmsgad.find(query);
          while (cursor.hasNext()) {
             Impmsgad s = (Impmsgad) JSON.parseObject(cursor.next().toString(),Impmsgad.class);
             String[] adlist=s.getAdlist().split(",");
             for(String ad:adlist) {
                 DBObject myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",ad));           
                 if(myDoc == null) {
                    out.write("<tr><td>impmsgad</td><td>name="+s.getName()+"</td><td>adrule="+ad+"</td></tr>");
                 }
             }
          }
          cursor.close();
// ACTION
         cursor = t_action.find(query);
         while (cursor.hasNext()) {
            Action s = (Action) JSON.parseObject(cursor.next().toString(),Action.class);
            DBObject myDoc= t_adanarule.findOne(new BasicDBObject("start",date).append("rule",s.getName()));
            if(myDoc == null) {
               out.write("<tr><td>action</td><td>name="+s.getName()+"</td><td>dimension NOT FOUND</td></tr>");
            } else {
               myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",s.getHigh()));
               if(myDoc == null) {
                   DBObject myDoc1= t_round.findOne(new BasicDBObject("start",date).append("name",s.getHigh()));
                   if(myDoc1 == null) {
                      out.write("<tr><td>action</td><td>name="+s.getName()+"</td><td>adrule="+s.getHigh()+" NOT FOUND</td></tr>");
                   }
               }
               myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",s.getMedium()));
               if(myDoc == null) {
                   DBObject myDoc1= t_round.findOne(new BasicDBObject("start",date).append("name",s.getMedium()));
                   if(myDoc1 == null) {
                      out.write("<tr><td>action</td><td>name="+s.getName()+"</td><td>adrule="+s.getMedium()+" NOT FOUND</td></tr>");
                   }
               }
               myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",s.getLow()));
               if(myDoc == null) {
                   DBObject myDoc1= t_round.findOne(new BasicDBObject("start",date).append("name",s.getLow()));
                   if(myDoc1 == null) {
                      out.write("<tr><td>action</td><td>name="+s.getName()+"</td><td>adrule="+s.getLow()+" NOT FOUND</td></tr>");
                   }
               }
            }
         }
         cursor.close();
// GROUP
          cursor = t_groupad.find(query);
          while (cursor.hasNext()) {
             Groupad s = (Groupad) JSON.parseObject(cursor.next().toString(),Groupad.class);
             DBObject myDoc= t_adanarule.findOne(new BasicDBObject("start",date).append("rule",s.getName()));
             if(myDoc==null) {
                out.println("<tr><td>GROUP</td><td>rule="+s.getName()+"</td><td>adanarule NOT FOUND</td>");
             } else {
                myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",s.getHigh()));
                if(myDoc==null) {
                   out.println("<tr><td>GROUP</td><td>rule="+s.getName()+"</td><td>adrule="+s.getHigh()+"NOT FOUND</td>");
                }
                myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",s.getMedium()));
                if(myDoc==null) {
                   out.println("<tr><td>GROUP</td><td>rule="+s.getName()+"</td><td>adrule="+s.getMedium()+"NOT FOUND</td>");
                }
                myDoc= t_adrule.findOne(new BasicDBObject("start",date).append("adrule",s.getLow()));
                if(myDoc==null) {
                   out.println("<tr><td>GROUP</td><td>rule="+s.getName()+"</td><td>adrule="+s.getLow()+"NOT FOUND</td>");
                }
             }
          }
       } catch (Exception ex) {
          out.println(ex.getMessage());
       }
       out.println("<tr><td colspan=3><a href='./Slist?modify="+date+"'>回setting主選單</a></td></tr></table>");
       out.println("</body></HTML>");
    }
}
